package com.apestech.api.workflow.lib.app;

import com.alibaba.druid.pool.DruidDataSource;
import com.apestech.api.workflow.extend.AddSign;
import com.apestech.api.workflow.extend.FlowDatabase;
import com.apestech.api.workflow.lib.FlowUtil;
import com.apestech.api.workflow.lib.element.FlowExecute;
import com.apestech.api.workflow.lib.element.FlowTask;
import com.apestech.api.workflow.lib.element.WorkProcessEngine;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.DBUtil;
import com.apestech.framework.util.modul.DBInfo;

import java.sql.Connection;

/**
 * 加签相关
 *
 * @author hup
 * @create 2018-07-21-14:32
 */
public class AddSignApp extends FlowUtil{

    private WorkProcessEngine engine;
    private SqlSession session;
    private FlowTask flowTask;
    private JSONObject input;
    private String processInstanceId;
    private String state;
    private String taskId;
    private String taskName;
    private JSONObject taskMap;
    private String nodeUserId;
    private String nodeUserName;
    private String loginUserId;
    private String userId;
    private String userName;

    public AddSignApp(WorkProcessEngine engine, JSONObject input) throws Exception {
        this.engine=engine;
        this.session= DBUtil.getSession(this.engine.getDataSource());
        this.session.setConnection(this.engine.getConnection());
        this.input=input;
        this.processInstanceId=input.getString("processInstanceId");
        this.taskId=input.getString("taskId");
        if(this.processInstanceId==null && this.taskId==null){
            throw new Exception("流程ID或任务ID不允许为空！");
        }
        preInputValidate(this.input, true);
        this.flowTask= getFlowTask(this.engine,  this.input);
        if(this.flowTask.getTask().isSuspended()){
            throw new Exception("该任务为挂起状态，不允许做加签相关操作！");
        }

        this.input.put("processInstanceId", flowTask.getProcessInstanceId());
        this.state=flowTask.getState();
        this.taskId=flowTask.getTask().getId();
        this.taskName=flowTask.getTask().getName();
        this.taskMap=getTaskMap(this.engine, this.taskId, this.input);
        this.nodeUserId=this.taskMap.getString("addUserId");
        this.nodeUserName=this.taskMap.getString("addUserName");
        this.userId=this.input.getString("userId");
        this.userName=this.input.getString("userName");
        this.loginUserId=this.input.getString("loginUserId");
    }

    /**
     * 加签发起
     * @throws Exception
     */
    public void startAddSign() throws Exception {
        if(!("claim".equals(state) || "complete".equals(state) || "assgin".equals(state) )){
            throw new Exception("当前状态不允许做加签发起操作！");
        }
        if("assgin".equals(state)){
            engine.getTaskService().claim(flowTask.getTask().getId(), input.getString("loginUserId"));
            flowTask.setState("claim");
            flowTask= getFlowTask(engine,  this.input);
        }
        nodeUserId=input.getString("loginUserId");
        nodeUserName=input.getString("loginUserName");
        if(!nodeUserId.equals(flowTask.getTask().getAssignee())){
            throw new Exception("当前结点发起人["+nodeUserId+"]不是结点经办人["+flowTask.getTask().getAssignee()+"]，请重新发起！");
        }

        userId=input.getString("userId");
        userName=input.getString("userName");
        FlowDatabase.UserInfo user=getCommission(engine, userId, userName);

        AddSign sign=new AddSign();
        sign.start(nodeUserId, nodeUserName, userId,  userName, user.getId(), user.getName());
        taskMap.put("claim", sign.getClaimStr());
        taskMap.put("addUserId", nodeUserId);
        taskMap.put("addUserName", nodeUserName);

        userId=user.getId();
        userName=user.getName();
        input.put("userId", userId);
        input.put("userName", userName);

        FlowExecute exec=new FlowExecute(engine);
        if(flowTask.getTask().getAssignee()!=null){
            exec.unClaimTask(taskId);
        }
        exec.deleteTaskAllUser(taskId);
        exec.claimTask(taskId, userId);

        engine.getTaskService().setVariables(taskId, taskMap);
        writeTaskDb(engine, session, input, taskId, taskName, ExtendDbType.START_ADD_SIGN);
        flowTask.setState("addSign");
    }

    /**
     * 加签
     * @throws Exception
     */
    public void addSign() throws Exception {
        if(!("addSign".equals(state))){
            throw new Exception("当前状态不允许做加签操作！");
        }

        userId=input.getString("userId");
        userName=input.getString("userName");
        FlowDatabase.UserInfo user=getCommission(engine, userId, userName);

        AddSign sign=new AddSign();
        sign.setClaim(taskMap.getString("claim"));
        sign.addUser(loginUserId, userId,  userName, user.getId(), user.getName());
        taskMap.put("claim", sign.getClaimStr());

        userId=user.getId();
        userName=user.getName();
        input.put("userId", userId);
        input.put("userName", userName);

        FlowExecute exec=new FlowExecute(engine);
        if(flowTask.getTask().getAssignee()!=null){
            exec.unClaimTask(taskId);
        }
        exec.deleteTaskAllUser(taskId);
        exec.claimTask(taskId, userId);

        engine.getTaskService().setVariables(taskId, taskMap);
        writeTaskDb(engine, session, input, taskId, taskName, ExtendDbType.START_ADD_SIGN);
    }

    /**
     * 加签执行
     * @throws Exception
     */
    public void execAddSign() throws Exception {
        if(!("addSign".equals(state))){
            throw new Exception("当前状态不允许做加签回应操作！");
        }
        AddSign sign=new AddSign();
        sign.setClaim(taskMap.getString("claim"));
        String id=sign.execUser(loginUserId);
        id=getCommission(engine, id);
        FlowExecute exec=new FlowExecute(engine);
        if(flowTask.getTask().getAssignee()!=null){
            exec.unClaimTask(taskId);
        }
        exec.deleteTaskAllUser(taskId);
        exec.claimTask(taskId, id);
        if(sign.isFinish()){
            engine.getTaskService().removeVariable(taskId, "claim");
            engine.getTaskService().removeVariable(taskId, "addUserId");
            engine.getTaskService().removeVariable(taskId, "addUserName");
            flowTask.setState("claim");
        } else {
            taskMap.put("claim", sign.getClaimStr());
            engine.getTaskService().setVariables(taskId, taskMap);
        }
        writeTaskDb(engine, session, input, taskId, taskName, ExtendDbType.ADD_SIGN);
    }

    /**
     * 加签转签
     * @throws Exception
     */
    public void changeSign() throws Exception {
        if(!("addSign".equals(state))){
            throw new Exception("当前状态不允许做加签转签操作！");
        }
        userId=input.getString("userId");
        userName=input.getString("userName");
        FlowDatabase.UserInfo user=getCommission(engine, userId, userName);

        AddSign sign=new AddSign();
        sign.setClaim(taskMap.getString("claim"));
        sign.changeUser(loginUserId, userId, userName, user.getId(), user.getName());

        userId=user.getId();
        userName=user.getName();
        input.put("userId", userId);
        input.put("userName", userName);

        FlowExecute exec=new FlowExecute(engine);
        if(flowTask.getTask().getAssignee()!=null){
            exec.unClaimTask(taskId);
        }
        exec.deleteTaskAllUser(taskId);
        exec.claimTask(taskId, userId);

        taskMap.put("claim", sign.getClaimStr());
        engine.getTaskService().setVariables(taskId, taskMap);
        writeTaskDb(engine, session, input, taskId, taskName,ExtendDbType.ADD_CHANGE_SIGN);
    }

}
